<template>
  <a-drawer
    :title="title"
    placement="right"
    :closable="true"
    :maskClosable="true"
    @close="onDrawerClose"
    :visible="visible"
    width="50%"
    :keyboard="true"
    :getContainer="false"
  >
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="entity" :rules="rules" v-bind="layout">
        <a-form-model-item label="角色名称" prop="RoleName">
          <a-input v-model="entity.RoleName" autocomplete="off"/>
        </a-form-model-item>
        <div :style="{height: getRoleListHeight,overflow: 'auto'}">
          <a-form-model-item label="权限信息" prop="AppSecret">
            <a-tree
              :showLine="true"
              v-if="actionsTreeData && actionsTreeData.length"
              :checkable="true"
              @check="onCheck"
              :autoExpandParent="true"
              v-model="checkedKeys"
              :treeData="actionsTreeData"
              :defaultExpandAll="true"
              :checkStrictly="true"
            >
            </a-tree>
          </a-form-model-item>
        </div>
      </a-form-model>
    </a-spin>
    <div
      :style="{
        position: 'absolute',
        right: 0,
        bottom: 0,
        width: '100%',
        borderTop: '1px solid #e9e9e9',
        padding: '10px 16px',
        background: '#fff',
        textAlign: 'center',
        zIndex: 1
      }">
      <a-button style="margin-right: 100px" @click="onDrawerClose">取消</a-button>
      <a-button type="primary" @click="handleSubmit">保存</a-button>
    </div>
  </a-drawer>
</template>

<script>
import TreeHelper from '@/utils/helper/TreeHelper'

export default {
  props: {
    afterSubmit: {
      type: Function,
      default: null
    }
  },
  data () {
    return {
      layout: {
        labelCol: { span: 5 },
        wrapperCol: { span: 18 }
      },
      tableHeight: 570, // 表格高度
      visible: false,
      confirmLoading: false,
      entity: {},
      title: '',
      actionsTreeData: [],
      allActionList: [],
      checkedKeys: { checked: [] },
      rules: {
        RoleName: [{ required: true, message: '必填' }]
      }
    }
  },
  mounted () {
    this.$nextTick(() => {
      // 页面渲染完成后的回调
      this.tableHeight = this.getTableHeight(false) + 120
    })
  },
  computed: {
    // 滚动区高度
    getRoleListHeight: function () {
      return this.tableHeight + 'px' // 自定义高度需求
    }
  },
  methods: {
    init () {
      this.visible = true
      this.entity = {}
      this.$nextTick(() => {
        this.$refs['form'].clearValidate()
      })

      this.$http.post('/Base_Manage/Base_Action/GetActionTreeList', {}).then(resJson => {
        if (resJson.Success) {
          this.actionsTreeData = resJson.Data
        }
      })
      this.$http.post('/Base_Manage/Base_Action/GetAllActionList', {}).then(resJson => {
        if (resJson.Success) {
          this.allActionList = resJson.Data
        }
      })
    },
    openDrawer (id, title) {
      this.init()
      this.title = title
      if (id) {
        this.$http.post('/Base_Manage/Base_Role/GetTheData', { id: id }).then(resJson => {
          this.entity = resJson.Data

          this.checkedKeys = { checked: this.entity['Actions'] }
        })
      }
    },
    onDrawerClose () {
      this.visible = false
    },
    onCheck (checkedKeys, e) {
      // //console.log('勾选')
      // //console.log(checkedKeys)
      // //console.log(this.checkedKeys)
      let children
      // 勾选事件,勾选节点时同时勾选所有父节点和子节点
      const value = e.node.value
      let newChecked = []
      if (e.checked) {
        const parentIds = TreeHelper.getParentIds(value, this.allActionList)
        children = TreeHelper.getChildrenIds(value, this.allActionList)
        const addNodes = parentIds.concat(children).filter(item => !this.checkedKeys.checked.includes(item))
        newChecked = this.checkedKeys.checked.concat(addNodes)
      } else {
        // 取消勾选事件,取消勾选所有子节点
        children = TreeHelper.getChildrenIds(value, this.allActionList)
        children.push(value)
        newChecked = this.checkedKeys.checked.filter(item => !children.includes(item))
      }

      this.checkedKeys = { checked: newChecked }
    },
    handleSubmit () {
      this.$refs['form'].validate(valid => {
        if (!valid) {
          return
        }
        this.confirmLoading = true
        this.entity['Actions'] = this.checkedKeys.checked
        this.$http.post('/Base_Manage/Base_Role/SaveData', this.entity).then(resJson => {
          this.confirmLoading = false

          if (resJson.Success) {
            this.$message.success('操作成功!')
            this.afterSubmit()
            this.visible = false
          } else {
            this.$message.error(resJson.Msg)
          }
        })
      })
    }
  }
}
</script>
<style lang="less" scoped>
/deep/.ant-tree {
  margin: 0 0 50px 0;
  padding: 0;
}
/deep/.ant-tree ul {
  margin: 0;
  padding: 0;
}

/deep/ .ant-tree ul li {
  padding: 0;
  margin: 0 auto;
  display: flex;
  flex-direction: row;
}
</style>
